﻿#region File Description
/****************************************************************************
*                                                                           *
* Game Manager.                                                             *
*                                                                           *
* Copyright (c) Ionescu Marius. All rights reserved(2013).                  *
*                                                                           *
****************************************************************************/
#endregion
namespace Silverlight3D
{
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Reflection;
    using System.IO;
    using System.Windows.Resources;
    using System;

    public class Cube
    {
        public Vector center;
        public double edgeLength;

        private IList<Vector> corners;

        private Image[] contentImages;
        private Size[] contentSizes;

        public Cube(Vector center, double edgeLength, string imageFilename)
        {
            this.center = center;
            this.edgeLength = edgeLength;

            this.corners = new List<Vector>();
            this.corners.Add(center + new Vector(edgeLength / 2, edgeLength / 2, edgeLength / 2));
            this.corners.Add(center + new Vector(-edgeLength / 2, edgeLength / 2, edgeLength / 2));
            this.corners.Add(center + new Vector(-edgeLength / 2, -edgeLength / 2, edgeLength / 2));
            this.corners.Add(center + new Vector(edgeLength / 2, -edgeLength / 2, edgeLength / 2));
            this.corners.Add(center + new Vector(edgeLength / 2, -edgeLength / 2, -edgeLength / 2));
            this.corners.Add(center + new Vector(-edgeLength / 2, -edgeLength / 2, -edgeLength / 2));
            this.corners.Add(center + new Vector(-edgeLength / 2, edgeLength / 2, -edgeLength / 2));
            this.corners.Add(center + new Vector(edgeLength / 2, edgeLength / 2, -edgeLength / 2));

            this.contentImages = new Image[6];
            this.contentSizes = new Size[6];

            this.SetImages(imageFilename);
        }

        public void AddToSceneWithVectorPolygons(Scene scene)
        {
            var side1 = this.GetVectorPolygonSide(0, 1, 2, 3);
            var side2 = this.GetVectorPolygonSide(4, 5, 6, 7);
            var side3 = this.GetVectorPolygonSide(7, 6, 1, 0);
            var side4 = this.GetVectorPolygonSide(5, 4, 3, 2);
            var side5 = this.GetVectorPolygonSide(6, 5, 2, 1);
            var side6 = this.GetVectorPolygonSide(0, 3, 4, 7);

            scene.Add(side1);
            scene.Add(side2);
            scene.Add(side3);
            scene.Add(side4);
            scene.Add(side5);
            scene.Add(side6);
        }

        private VectorPolygon GetVectorPolygonSide(int corner0, int corner1, int corner2, int corner3)
        {
            var side = new VectorPolygon();

            side.Vectors.Add(this.corners[corner0]);
            side.Vectors.Add(this.corners[corner1]);
            side.Vectors.Add(this.corners[corner2]);
            side.Vectors.Add(this.corners[corner3]);

            side.StrokeColor = Color.FromArgb(255, 224, 224, 255);
            side.StrokeThickness = 1.0;
            side.FillColor = Color.FromArgb(255, 32, 32, 220);
            side.BrightnessFactor = 0.65;
            side.HideIfBackFacing = true;

            return side;
        }

        public void AddToSceneWithImageSides(Scene scene)
        {
            var side1 = this.GetImageSide(0, 1, 3, 0);
            var side2 = this.GetImageSide(4, 5, 7, 1);
            var side3 = this.GetImageSide(7, 6, 0, 2);
            var side4 = this.GetImageSide(5, 4, 2, 3);
            var side5 = this.GetImageSide(6, 5, 1, 4);
            var side6 = this.GetImageSide(0, 3, 7, 5);

            scene.Add(side1);
            scene.Add(side2);
            scene.Add(side3);
            scene.Add(side4);
            scene.Add(side5);
            scene.Add(side6);
        }

        private VectorRect GetImageSide(int corner0, int corner1, int corner3, int index)
        {
            var side = new ShadedVectorRect();
            side.Vector0 = this.corners[corner0];
            side.Vector1 = this.corners[corner1];
            side.Vector3 = this.corners[corner3];

            side.HideIfBackFacing = true;

            side.Content = this.contentImages[index];
            side.ContentSize = this.contentSizes[index];
            return side;
        }

        public void SetImages(string imageFilename)
        {
            for (int i = 0; i < 6; i++)
            {
                BitmapImage bitmap = ResourceHelper.GetBitmap(@"\Assets\Images\" + imageFilename, Assembly.GetExecutingAssembly());

                this.contentImages[i] = new Image();
                this.contentImages[i].Stretch = Stretch.None;
                this.contentImages[i].Source = bitmap;

                this.contentSizes[i] = new Size(bitmap.PixelWidth, bitmap.PixelHeight);
            }
        }
    }
}